import myStroe from "@/store"
import * as api from "@/api"
import __ from 'lodash'
import moment from 'moment'
export function keyboardOp(item) {
  let event = event || window.event;
  switch (event.keyCode) {
    case 37: // 按下左箭头键，向左移动10个像素
      item.x = item.x - 10;
      break;
    case 39: // 按下右箭头键
      item.x = item.x + 10;
      break;
    case 38: // 按下上箭头键
      item.y = item.y - 10;
      break;
    case 40: // 按下下箭头键
      item.y = item.y + 10;
      break;
  }
  return false;
}

function ArrayStack() {
  const arr = [];
  this.push = function (element) {
    arr.push(element);
  }
  this.pop = function () {
    return arr.pop();
  }
  this.top = function () {
    return arr[arr.length - 1];
  }
  this.size = function () {
    return arr.length;
  }
  this.clear = function () {
    arr = [];
    return true;
  }
  this.toString = function () {
    return arr.toString();
  }
}
const saveData = new ArrayStack();
const opData = new ArrayStack();
export function getData(arr) {
  saveData.push(arr)
}
export function isOp(flag) {
  if (flag) {
    return flag
  }
  return false
}
export function op(cb) {
  if (event.ctrlKey && event.keyCode === 83) {
    event.returnValue = false;
    if (myStroe.state.home.showList.length === 0) {
      return
    }
    if (myStroe.state.home.oldListId === -1) {
      myStroe.state.home.dialogFormVisible = true;
    } else {
      //获取当前时间
      let nowDate = new Date().getTime()
      let obj = {
        data: myStroe.state.home.showList,
        id: myStroe.state.home.oldListId,
        dateTime: nowDate,
      }
      let res = api.add(obj)
      myStroe.state.home.oldListId = res.id;
    }
    // __.debounce(api.add(obj),30000)
  } else if (event.ctrlKey && event.keyCode === 90) {
    //后退
    isOp('back')
    let tmpArr = saveData.pop()
    if (tmpArr === undefined) {
      myStroe.state.home.showList = []
      return
    }
    opData.push(tmpArr);
    myStroe.state.home.showList = tmpArr
  } else if (event.ctrlKey && event.keyCode === 89) {
    //前进
    isOp('forward')
    let tmpArr = opData.pop()
    if (tmpArr === undefined) {
      return
    }
    saveData.push(tmpArr);
    myStroe.state.home.showList = tmpArr
  }
}
